/// <reference path="../_all.ts" />

module Application.Controllers {

    export class LoginController implements Application.Interfaces.Controllers.IController {

        public static $inject = [
            '$rootScope',
            '$scope',
            '$location',
            'UserService',
            'TranslationService',
        ];

        loginResult: {
            (data): void;
            (data, err): void;
        };

        translationResult: {
            (data): void;
            (data,err): void;
        };

        constructor(public RootScope: Application.Interfaces.Scopes.IRootScope
            , public Scope: Application.Interfaces.Scopes.ILoginScope
            , public Location: ng.ILocationService
            , public Users: Application.Interfaces.Services.IUserService
            , public Translations: Application.Interfaces.Services.ITranslationService) {
            this.Scope.vm = this;
            this.Scope.location = this.Location;
            this.Scope.messages = new Application.Utility.Messages();
            this.Scope.navigations = new Application.Utility.Navigations();

            this.Scope.navigations.AddLink("Login", "/login", true, false);
            
            this.loginResult = (data, err?) => {
                this.loginResultProxy.apply(this, [data, err]);
            }

            this.translationResult = (data, err?) => {
                this.translationResultProxy.apply(this, [data, err]);
            }
       }

        public log(msg: string) {
            //console.log(msg);
        }

        public login() {
            this.Scope.messages.Clear();
            this.log('login');
            this.Users.GetSession(this.Scope.user.login.toLowerCase(), this.Scope.user.password, this.loginResult);
        }

        private loginResultProxy(data, err) {
            this.log('login result');
            if (data != null) {
                var user = <Application.Resources.IUser> data;
                if (user != null && user.login.toLowerCase() == this.Scope.user.login.toLowerCase()) {
                    Application.Resources.UserUtility.fixBooleans(user);
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.success, "Info", new Application.Utility.String(this.Translations.Translate('USERLOGGEDIN')).format(user.login));
                    if (user.role > 0) {
                        this.RootScope.user = user;
                        this.Translations.GetTranslation(user.lang || 'en', this.translationResult);
                    };
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, "Error", "User NOT found");
                };
            } else {
                if (err != null) {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, err.data.status + ' ' + err.data.type, err.data.message);
                } else {
                    this.Scope.messages.AddAndGrowl(Application.Utility.MessageType.error, '000 Unknown', 'An unknown error occured');
                };
            };
        }

        private translationResultProxy(data, err) {
            this.log('translation result');
            this.Location.path('/servers');
        }
    };
}